import React, {  PureComponent } from 'react'
import store from '../../redux/store';
import { bindActionCreators } from 'redux'
import {connect} from "react-redux"
import { simpleSetData } from '../../redux/actions'
import { simpleSetUser,getSagaList,deleteSagaListItem } from '../../redux/actions/user'
import { List }  from 'antd'
let mapStateProps = state =>{
    return {
        demoNum:state.org.demoNum,
        currentUser:state.user.currentUser,
        sagaList:state.user.sagaList
    }
}
let mapDispatchToProps = dispatch => bindActionCreators({simpleSetData,getSagaList,deleteSagaListItem},dispatch)


class TodoList extends PureComponent{
    constructor(props){
        super(props);
    }
    setUserByThunk(){
        setTimeout(()=>{
            const action = simpleSetUser({name:'real'});
            store.dispatch(action);
        },600);
    }
    getListBySaga(){
        setTimeout(() => {
           const action = getSagaList();
           store.dispatch(action);
        },400);
    }
    componentDidMount(){
        this.setUserByThunk();
        this.getListBySaga();
    }
    deleteItem=(index)=> {
        const action = deleteSagaListItem(index);
        store.dispatch(action);

    }
    render(){
        let { demoNum,currentUser,sagaList } = this.props;
        console.log(sagaList);
        return (
            <>
               {currentUser ? (<h2>{currentUser.name}</h2>):(<h2>登陆中..</h2>)}
                <h1 onClick={()=>simpleSetData(++demoNum)}>{demoNum}</h1>
                <div style={{margin:'10px'}}>
                    <div style={{margin:'10px',width:'300px'}}>
                        <List
                            bordered={true}
                            dataSource={sagaList}
                            renderItem={(item,index)=>(<List.Item onClick={()=>this.deleteItem(index)}>{item.title}</List.Item>)}
                        />    
                    </div>
                </div>
            </>
        )
    }
}

export default connect(mapStateProps,mapDispatchToProps)(TodoList);